package ssoUserLoginService

import (
	"gitee.com/krio/ginc/example/core/exerr"
	exampleDtoApiPbSsoUserLogin "gitee.com/krio/ginc/example/dto/api/pb/sso/user_login"
	"github.com/click33/sa-token-go/stputil"
	"github.com/gin-gonic/gin"
)

// UserLogin 登录
func UserLogin(ctx *gin.Context, req *exampleDtoApiPbSsoUserLogin.UserLoginReq) (*exampleDtoApiPbSsoUserLogin.UserLoginResp, error) {
	if req.LoginId == "" {
		return nil, exerr.ParamInvalid.WithSubmsg("登录ID必填")
	}
	if req.LoginDevice == "" {
		return nil, exerr.ParamInvalid.WithSubmsg("登录设备必填")
	}
	token, err := stputil.Login(req.LoginId, req.LoginDevice)
	if err != nil {
		return nil, err
	}
	return &exampleDtoApiPbSsoUserLogin.UserLoginResp{
		Token: token,
	}, nil
}

func CheckLogin(ctx *gin.Context, token string) error {
	err := stputil.CheckLogin(token)
	if err != nil {
		return err
	}
	return nil
}

// UserLogout 登出
func UserLogout(ctx *gin.Context, token string) error {
	if token == "" {
		return nil
	}
	err := stputil.LogoutByToken(token)
	if err != nil {
		return err
	}
	return nil
}

type LoginInfo struct {
	LoginId     string          // 登录Id
	LoginDevice LoginDeviceType // 登录设备
}

type LoginDeviceType string

const (
	PHONE LoginDeviceType = "phone"
	PC    LoginDeviceType = "pc"
	APP   LoginDeviceType = "app"
	ADMIN LoginDeviceType = "admin"
)

func (v LoginDeviceType) ToString() string {
	return string(v)
}
